home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPP05.ZIP / ANIMAL.CPP < prev    next >
C/C++ Source or Header  |  1991-07-04  |  2KB  |  105 lines

  1. // animal.cpp -- Inheritance example
  2.  
  3. //#include <stream.hpp>
  4. #include <iostream.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. #define FALSE     0
  9. #define TRUE      1
  10.  
  11. class animal {
  12.   private:
  13.     char name[30];    // The animal's name
  14.   public:
  15.     animal(const char *s);
  16.     const char *getName(void) { return name; }
  17. };
  18.  
  19. class mammal : public animal {
  20.   private:
  21.     int offspring;     // Number of offspring
  22.   public:
  23.     mammal(const char *s, int nc);
  24.     int numOffspring(void) { return offspring; }
  25. };
  26.  
  27. class bird : public animal {
  28.   private:
  29.     int eggs;         // Average number of eggs
  30.     int nesting;      // True if builds nest
  31.   public:
  32.     bird(const char *s, int ne, int nests);
  33.     int getEggs(void) { return eggs; }
  34.     const char *buildsNest()
  35.       { if (nesting) return "True"; else return "False"; }
  36. };
  37.  
  38. void showMammal(mammal &m);
  39. void showBird(bird &b);
  40.  
  41. main()
  42. {
  43.   mammal homoSapiens("Homo Sapiens", 1);
  44.   mammal gopher("Gopher", 9);
  45.   mammal armadillo("Armadillo", 4);
  46.   mammal houseMouse("House Mouse", 12);
  47.  
  48.   bird woodDuck("Wood Duck", 15, FALSE);
  49.   bird sandhillCrane("Sandhill Crane", 2, TRUE);
  50.   bird loon("Loon", 3, TRUE);
  51.  
  52.   cout << "\n\nMammals:";
  53.   showMammal(homoSapiens);
  54.   showMammal(gopher);
  55.   showMammal(armadillo);
  56.   showMammal(houseMouse);
  57.  
  58.   cout << "\n\nBirds:";
  59.   showBird(woodDuck);
  60.   showBird(sandhillCrane);
  61.   showBird(loon);
  62.  
  63.   exit(0);
  64. }
  65.  
  66. /* -- Display functions */
  67.  
  68. void showMammal(mammal &m)
  69. {
  70.   cout << "\nName ............... " << (m.getName());
  71.   cout << "\n Avg offspring ..... " << (m.numOffspring());
  72. }
  73.  
  74. void showBird(bird &b)
  75. {
  76.   cout << "\nName ............... " << (b.getName());
  77.   cout << "\n Avg no. eggs ...... " << (b.getEggs());
  78.   cout << "\n Builds a nest ..... " << (b.buildsNest());
  79. }
  80.  
  81. /* -- Class constructors */
  82.  
  83. animal::animal(const char *s)
  84.   strncpy(name, s, 29);
  85. }
  86.  
  87. mammal::mammal(const char *s, int nc) : animal(s)
  88. {
  89.   offspring = nc;
  90. }
  91.  
  92. bird::bird(const char *s, int ne, int nests) : animal(s)
  93. {
  94.   eggs = ne;
  95.   nesting = nests;
  96. }
  97.  
  98.  
  99. // Copyright (c) 1990 by Tom Swan. All rights reserved
  100. // Revision 1.00    Date: 09/24/1990   Time: 10:04 am
  101.  
  102. // Revision 1.01    Date: 07/03/1991   Time: 09:07 pm
  103. // Converted for Borland C++ 2.0
  104.